package com.loyal.mwm.libs.api;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.loyal.client.RxClient;
import com.loyal.client.RxConfig;
import com.base.kit.DevUtil;
import com.base.kit.GsonUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ServerApi {
    public static final String HTTP = "http";
    public static final String HTTPS = "https";
    /**
     * 公安网：tracall
     */
    public static final String NAMESPACE = "tracall";

    public static final String HOST = "lzcgs.axjt.net:80";
    public static final String MEDIA_TYPE = "application/json; charset=utf-8";

    public static final String DEFAULT_IP = "192.168.0.207";
    public static final String DEFAULT_PORT = "";

    public static RequestBody obj2Body(Object obj) {
        String objJson = GsonUtil.obj2Json(obj);
        return RequestBody.create(objJson, MediaType.parse(MEDIA_TYPE));
    }

    public static RequestBody createBody(String objJson) {
        return RequestBody.create(objJson, MediaType.parse(MEDIA_TYPE));
    }

    public static @NonNull
    RxConfig config() {
        RxConfig rxConfig = RxConfig.getInstance();
        ServerApiBean apiBean = DaoUtil.queryServerApi();
        String url = null == apiBean ? "" : apiBean.getUrl();
        String port = null == apiBean ? "" : apiBean.getPort();
        rxConfig.setHttpOrHttps(HTTP);
        rxConfig.setUrl(TextUtils.isEmpty(url) ? DEFAULT_IP : url);
        rxConfig.setPort(TextUtils.isEmpty(port) ? DEFAULT_PORT : port);
        //互联网-公安网
        rxConfig.setNameSpace(NAMESPACE);
        return rxConfig;
    }

    public static String getUpdateUrl(String version, String type) {
        RxConfig rxConfig = config();
        String baseUrl = RxClient.getBaseUrl(rxConfig);
        return String.format("%sjk/uploadApp?version=%s&type=%s", baseUrl, version, type);
    }

    /**
     * 微信公众号二维码地址
     */
    public static String getWxMpUrl() {
        RxConfig rxConfig = config();
        String baseUrl = RxClient.getBaseUrl(rxConfig);
        return String.format("%scall/getTempQRCode.do", baseUrl);
    }

    /**
     * token
     */
    public static Map<String, String> tokenMap(String username, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        return params;
    }

    /**
     * businessMenu
     */
    public static Map<String, String> menuMap(String sbip, String token) {
        Map<String, String> params = new HashMap<>();
        params.put("sbip", sbip);
        params.put("token", token);
        return params;
    }

    /**
     * 同步信息
     */
    public static Map<String, String> asyncMap() {
        Map<String, String> params = new HashMap<>();
        params.put("sbip", DevUtil.ipv4());
        return params;
    }

    /**
     * 检测代理人每天代理次数是否超过限制(>5次)
     *
     * @param dlrsfzmhm 代理人身份证号
     */
    public static Map<String, String> agentLimitMap(String dlrsfzmhm) {
        Map<String, String> params = new HashMap<>();
        params.put("dlrsfzmhm", dlrsfzmhm);
        return params;
    }

    /**
     * 保存排队叫号信息
     *
     * @param sqrXm UrlEncode加密
     * @param dlrXm UrlEncode加密
     */
    public static Map<String, String> savePhdJson(String ywlx, String sqrXm, String sqrSfzh, String dlrXm, String dlrSfzh, String rylb, String token, String sjhm, String yhlx) {
        Map<String, String> params = new HashMap<>();
        params.put("sbip", DevUtil.ipv4());
        params.put("pdhlb", "A");
        params.put("qhfs", "1");
        params.put("rylb", rylb);
        params.put("token", token);
        params.put("ywlb", ywlx);
        params.put("sqrxm", sqrXm);
        params.put("sfzmhm", sqrSfzh);
        params.put("dlrxm", dlrXm);
        params.put("dlrsfzmhm", dlrSfzh);
        params.put("sjhm", sjhm);
        params.put("yhlx", TextUtils.isEmpty(yhlx) ? "13" : yhlx);

        return params;
    }

    /**
     * 生成微信公众号二维码，
     *
     * @param xm Base64加密
     */
    public static Map<String, String> buildWxQrCode(String xm, String sfzmhm) {
        Map<String, String> params = new HashMap<>();
        params.put("xm", xm);
        params.put("sfzmhm", sfzmhm);
        return params;
    }

    /**
     * 检查是否有更新，
     */
    public static Map<String, String> update(String version, String type) {
        Map<String, String> params = new HashMap<>();
        params.put("version", version);
        params.put("type", type);

        return params;
    }
}